package com.ruoyi.project.module.phone.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.module.phone.domain.Phone;
import com.ruoyi.project.module.phone.service.IPhoneService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 手机 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-11-12
 */
@Controller
@RequestMapping("/module/phone")
public class PhoneController extends BaseController
{
    private String prefix = "module/phone";
	
	@Autowired
	private IPhoneService phoneService;
	
	@RequiresPermissions("module:phone:view")
	@GetMapping()
	public String phone()
	{
	    return prefix + "/phone";
	}
	
	/**
	 * 查询手机列表
	 */
	@RequiresPermissions("module:phone:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Phone phone,ModelMap mmap)

	{
		startPage();
        List<Phone> list = phoneService.selectPhoneList(phone);
		List<Phone> phoneType = phoneService.findPhoneType();
		mmap.put("phoneType",phoneType);
		for (String key : mmap.keySet()) {
			System.out.println("key= "+ key + " and value= " + mmap.get(key));
		}
		return getDataTable(list);
	}
	/**
	 * 新增手机
	 * 新增手机信息
	 *
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存手机
	 */
	@RequiresPermissions("module:phone:add")
	@Log(title = "手机", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Phone phone)
	{
		// 输出手机信息
		return toAjax(phoneService.insertPhone(phone));
	}

	/**
	 * 修改手机
	 */
	@GetMapping("/edit/{phoneId}")
	public String edit(@PathVariable("phoneId") Integer phoneId, ModelMap mmap)
	{
		Phone phone = phoneService.selectPhoneById(phoneId);
		mmap.put("phone", phone);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存手机
	 */
	@RequiresPermissions("module:phone:edit")
	@Log(title = "手机", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Phone phone)
	{		
		return toAjax(phoneService.updatePhone(phone));
	}
	
	/**
	 * 删除手机
	 */
	@RequiresPermissions("module:phone:remove")
	@Log(title = "手机", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(phoneService.deletePhoneByIds(ids));
	}
	
}
